package com.whosly.strategy.registrar.container;

import com.whosly.strategy.annotation.StrategyContainerInject;
import com.whosly.strategy.container.IStrategyContainer;
import com.whosly.strategy.registrar.AbstractBeanDefinitionRegistrar;
import com.whosly.strategy.scan.spring.StrategyContainerClassPathBeanDefinitionScanner;
import com.whosly.strategy.scan.spring.StrategyContainerTypeFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.filter.AnnotationTypeFilter;

/**
 * ContainerRegistrar 与 ContainerSpringRegistrar 二选一
 *
 * @Author yueny09 <deep_blue_yang@126.com>
 * @Date 2019-09-05 15:35
 */
@Lazy
@Slf4j
@Deprecated
public class ContainerSpringRegistrar extends AbstractBeanDefinitionRegistrar {

    @Override
    protected String getTaskName(){
        return "ContainerSpringRegistrar";
    }

    @Override
    protected void registerBeanDefinitions(AnnotationAttributes enableStrategy, BeanDefinitionRegistry registry) {
        String[] packagesToScan = getPackagesToScan(enableStrategy);

        StrategyContainerClassPathBeanDefinitionScanner scanner = new StrategyContainerClassPathBeanDefinitionScanner(registry);
        scanner.addIncludeFilter(new AnnotationTypeFilter(StrategyContainerInject.class));
        scanner.addIncludeFilter(new StrategyContainerTypeFilter(IStrategyContainer.class));

        scanner.scan(packagesToScan);
    }

    /**
     * 得到需扫描的包名
     * @return
     */
    private String[] getPackagesToScan(AnnotationAttributes enableStrategy) {
        String[] basePackages = enableStrategy.getStringArray("value");
        if(basePackages == null || basePackages.length == 0){
            return new String[]{BASE_PACKAGES};
        }

        return basePackages;
    }
}
